package com.yonyou;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.common.service.messagecenter.util.HttpClientUtil;

@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@Controller
public class MarketingServiceMqcenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(MarketingServiceMqcenterApplication.class, args);
	}
	@RequestMapping(value = "/mqtest")
    @ResponseBody
    public  String available(int threadCount,int mesCount) {

        long startTime=System.currentTimeMillis();

        MyRunnable runnable = new MyRunnable(mesCount);
        List<Thread> list = new ArrayList<Thread>();  
          
        for (int i = 1; i <= threadCount; i++)  
        {  
            Thread my = new Thread(runnable);
            try{
                my.start();  
            }catch(Exception e){
                e.printStackTrace();
            }
            list.add(my);  
        }  
  
        try  
        {  
            for (Thread my : list)  
            {  
                my.join();  
            }  
        }  
        catch (InterruptedException e)  
        {  
            e.printStackTrace();  
        }  
        long endTime=System.currentTimeMillis(); //获取结束时间  
        System.out.println("程序运行时间： "+(endTime-startTime)/1000+" s");  
        return "程序运行时间： "+(endTime-startTime)/1000+" s";
    }
    
    
    @RequestMapping(value = "/mqreceive", method = RequestMethod.POST)
    @ResponseBody
    public  String mqreceive() throws InterruptedException {
        System.out.println("--mqreceive");
        Thread.sleep(300l);
        return "{\"result\":\"200\"}";      
    }
}
class MyRunnable implements Runnable{
    private int count=1;
     
     public MyRunnable(int i) {
         count=i;
     }
     
     @Override
     public void run() {

            long startTime=System.currentTimeMillis();//10.180.8.212 

            String host="localhost:8002";//"172.16.198.66:8082 ";//
            String data="sadfsfsafsddddfsdffsadfsdfddddddddddd44444";
            String url="";//"http://"+host+":8002/message/exchange?channel=exchange1&appid=2&sign=00002dfed24a7328cce90f5706b9e807c&occurtime=2016-12-26&nonce=123 ";
            for(int i=1;i<=count;i++){
                url="http://"+host+"/message/exchange?channel=test&appid=2&sign="+i+"dfed24a7328cce90f5706b9e807c&occurtime=2016-12-26&nonce=123";
                HttpClientUtil.sendPostRequest(url, "{\"occurtime\":\""+data+"\"}");//+data+data+data+data
            }
            long endTime=System.currentTimeMillis(); //获取结束时间  
            System.out.println(Thread.currentThread().getId()+"程序运行时间： "+(endTime-startTime)+" ms");   
     }
}
